package com.lancoo.ccas53.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lancoo.ccas53.aspect.LogAnnotation;
import com.lancoo.ccas53.entity.Campus;
import com.lancoo.ccas53.entity.*;
import com.lancoo.ccas53.pojo.common.PageInfo;
import com.lancoo.ccas53.pojo.vo.*;
import com.lancoo.ccas53.service.*;
import com.lancoo.ccas53.util.PagingUtil;
import com.lancoo.ccas53.util.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName CommonController
 * @Description TODO
 * @Author quin
 * @Date 2024/5/21 11:02
 */
@Api(tags = "任务控制器")
@RestController
@RequestMapping("/common")
public class CommonController {

    @Resource
    private TaskInfoService taskInfoService;
    @Resource
    private CampusService campusService;
    @Resource
    private TaskProcessService taskProcessService;
    @Resource
    private UserSettingService userSettingService;
    @Resource
    private TypeService typeService;
    @Resource
    private CollegeService collegeService;
    @Resource
    private MajorGradeService majorGradeService;
    @Resource
    private StudentService studentService;
    @Resource
    private CourseService courseService;
    @Resource
    private RoomService roomService;
    @Resource
    private AssessmentMethodService assessmentMethodService;


    @ApiOperation("开启新学期计划")
    @PostMapping("/addOrUpdateTask")
    public ResponseObject<TaskInfo> createTask(@ApiParam(value = "任务", required = true) @RequestBody TaskInfo taskInfo) {
        return ResponseObject.ofSuccess(taskInfoService.addTask(taskInfo));
    }

//    @PostMapping("tempTodo")
//    public ResponseObject<Void> createTask(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
//        taskProcessService.initProcess(taskId);
//        return null;
//    }

    @ApiOperation("更新新学期计划")
    @PostMapping("/updateTask")
    public ResponseObject<TaskInfo> updateTask(@ApiParam(value = "任务", required = true) @RequestBody TaskInfo taskInfo) {
        return ResponseObject.ofSuccess(taskInfoService.updateTask(taskInfo));
    }

    @ApiOperation("获取全部计划")
    @GetMapping("/getTaskList")
    public ResponseObject<PageInfo<TaskInfo>> getTaskList(@ApiParam(value = "学校id", required = true) @RequestParam String schoolId,
                                                          @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                          @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {

        Page<TaskInfo> page = new Page<>(currentPage, pageSize);
        Page<TaskInfo> taskInfoPage = taskInfoService.lambdaQuery().eq(TaskInfo::getSchoolId, schoolId).orderByAsc(TaskInfo::getTerm).page(page);
        return ResponseObject.ofSuccess(PagingUtil.formatPageResult(taskInfoPage));
    }

    @ApiOperation("根据id删除计划")
    @DeleteMapping("/removeTaskByIds")
    public ResponseObject<Object> removeTaskByIds(@ApiParam(value = "任务ids", required = true) @RequestBody List<Integer> taskIds) {
        taskInfoService.removeTaskByIds(taskIds);
        return ResponseObject.ofSuccess("删除成功");
    }

    @ApiOperation("获取周次信息")
    @GetMapping(value = "/week/{taskId}")
    public ResponseObject<WeekDateVo> countWeek(@ApiParam(value = "任务Id", required = true) @PathVariable Integer taskId) {
        return ResponseObject.ofSuccess(taskInfoService.countWeek(taskId));
    }

    @ApiOperation("任务各阶段状态")
    @GetMapping(value = "/taskProgress/{taskId}")
    public ResponseObject<List<TaskProcess>> getTaskProgress(@ApiParam(value = "任务Id", required = true) @PathVariable Integer taskId) {
        return ResponseObject.ofSuccess(taskProcessService.getProcessList(taskId));
    }

    @ApiOperation("修改任务阶段状态")
    @GetMapping(value = "/taskProgress/update")
    public ResponseObject<Boolean> updateTaskProgress(@ApiParam(value = "计数", required = true) @RequestParam Integer record,
                                                      @ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                      @ApiParam(value = "0未完成  1已完成", required = true) @RequestParam Integer finish) {
        return ResponseObject.ofSuccess(taskProcessService.updateProcess(record, taskId, finish));
    }

    @ApiOperation("获取任务阶段状态")
    @GetMapping(value = "/taskProgress/getProcess")
    public ResponseObject<TaskProcess> getProcess(@ApiParam(value = "计数", required = true) @RequestParam Integer record,
                                                  @ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(taskProcessService.getProcess(record, taskId));
    }

    @ApiOperation("获取用户配置")
    @GetMapping(value = "/getSidebar")
    public ResponseObject<UserSetting> getUserSetting(@ApiParam(value = "用户id", required = true) @RequestHeader String userId) {
        UserSetting userSetting = userSettingService.getUserSettingByUserId(userId);
        return ResponseObject.ofSuccess(userSetting);
    }

    @ApiOperation("设置用户侧边栏配置")
    @PostMapping(value = "/setSidebar")
    public ResponseObject<Boolean> addOrUpdateSetting(@ApiParam(value = "用户配置类", required = true) @RequestBody UserSetting userSetting) {
        return ResponseObject.ofSuccess(userSettingService.updateUserSetting(userSetting));
    }

    @ApiOperation("获取校区")
    @GetMapping("/getCampus")
    public ResponseObject<List<Campus>> getCampusList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(campusService.lambdaQuery().eq(Campus::getTaskId, taskId).list());
    }

    @ApiOperation("设置校区")
    @PostMapping("/setCampus")
    public ResponseObject<List<Campus>> setCampus(@ApiParam(value = "校区列表", required = true) @RequestBody List<Campus> campuses) {
        campusService.updateBatchById(campuses);
        return ResponseObject.ofSuccess(campuses);
    }

    @ApiOperation("获取类型")
    @GetMapping("/getType")
    public ResponseObject<List<Type>> getType(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                              @ApiParam(value = "1、活动类型 2、场地类型 3、授课类型", required = true) @RequestParam Integer typeMode) {
        return ResponseObject.ofSuccess(typeService.getType(taskId, typeMode));
    }

    @ApiOperation("删除类型")
    @DeleteMapping("/removeType")
    public ResponseObject<Boolean> getType(@ApiParam(value = "类型id", required = true) @RequestParam Integer typeId) {
        return ResponseObject.ofSuccess(roomService.getRoomTypeById(typeId));
    }

    @ApiOperation("添加类型")
    @PostMapping("/addType")
    public ResponseObject<Type> getType(@ApiParam(value = "实体类", required = true) @RequestBody Type type) {
        typeService.save(type);
        return ResponseObject.ofSuccess(type);
    }


    @ApiOperation("获取学院及各学院提交进度")
    @GetMapping("/getCollege")
    public ResponseObject<List<College>> getCollege(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        List<College> collegeList = collegeService.lambdaQuery().eq(College::getTaskId, taskId).list();
        return ResponseObject.ofSuccess(collegeList);
    }

    @ApiOperation("修改学院提交进度")
    @PostMapping("/setCollegeProgress")
    public ResponseObject<Boolean> setCollegeProgress(@ApiParam(value = "实体类", required = true) @RequestBody College college) {
        collegeService.updateById(college);
        return ResponseObject.ofSuccess(true);
    }

    @ApiOperation("获取专业")
    @GetMapping("/getMajor")
    public ResponseObject<List<MajorVo>> getMajor(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                  @ApiParam(value = "基础平台学院id", required = false) @RequestParam(required = false) String baseCollegeId) {
        return ResponseObject.ofSuccess(majorGradeService.getMajor(taskId, baseCollegeId));
    }

    @ApiOperation("获取年级")
    @GetMapping("/getGrade")
    public ResponseObject<List<GradeVo>> getGrade(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                  @ApiParam(value = "学院id", required = false) @RequestParam(required = false) String baseCollegeId) {
        return ResponseObject.ofSuccess(majorGradeService.getGrade(taskId, baseCollegeId));
    }

    @ApiOperation("获取班级")
    @GetMapping(value = "/getClass")
    public ResponseObject<List<ClassVo>> getClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                  @ApiParam(value = "学院id", required = false) @RequestParam(required = false) String baseCollegeId,
                                                  @ApiParam(value = "专业id", required = false) @RequestParam(required = false) String majorId,
                                                  @ApiParam(value = "年级id", required = false) @RequestParam(required = false) String gradeId) {
        return ResponseObject.ofSuccess(studentService.getClassByTaskId(taskId, baseCollegeId, majorId, gradeId));
    }

    @ApiOperation("获取课程列表")
    @GetMapping(value = "/getCourseList")
    public ResponseObject<List<Course>> getCourseList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                      @ApiParam(value = "基础平台学院id") @RequestParam(required = false) String baseCollegeId,
                                                      @ApiParam(value = "课程类别id") @RequestParam(required = false) String courseTypeId,
                                                      @ApiParam(value = "课程性质 1必修 2选修") @RequestParam(required = false) Integer courseNature,
                                                      @ApiParam(value = "1、课程 2、环节") @RequestParam(required = false) Integer flag,
                                                      @ApiParam(value = "课程名模糊查") @RequestParam(required = false) String keyword,
                                                      @ApiParam(value = "4、不选课+在线选课课程 5、显示不选课+预选选课课程") @RequestParam(required = false) Integer isSelect,
                                                      @ApiParam(value = "操作状态 0未开启 1已开启") @RequestParam(required = false) Integer isEnable) {
        return ResponseObject.ofSuccess(courseService.getCourseList(taskId, baseCollegeId, courseTypeId, courseNature, flag, keyword, isSelect, isEnable));
    }

    @ApiOperation("获取专业年级树")
    @GetMapping(value = "/getMajorGradeTree")
    public ResponseObject<List<CollegeTree>> getMajorGradeTree(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                               @ApiParam(value = "专业关键字模糊查询") @RequestParam(required = false) String keyword,
                                                               @ApiParam(value = "1、主修 2、辅修", required = true) @RequestParam Integer studyNature) {
        return ResponseObject.ofSuccess(majorGradeService.getMajorGradeTree(taskId, keyword, studyNature));
    }


    @GetMapping("/getBuildingList")
    @ApiOperation("获取楼宇")
    @LogAnnotation(operationType = "3", operationContent = "获取楼宇")
    public ResponseObject<List<Room>> getBuildingList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                      @ApiParam(value = "校区id", required = false) @RequestParam(required = false) Integer campusId) {
        return ResponseObject.ofSuccess(roomService.getBuildingList(taskId, campusId));
    }


    @GetMapping("/getBuildingListV2")
    @ApiOperation("获取楼宇V2")
    @LogAnnotation(operationType = "3", operationContent = "获取楼宇")
    public ResponseObject<List<Room>> getBuildingListV2(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(roomService.getBuildingListV2(taskId));
    }

    @GetMapping("/getBuildingFloor")
    @ApiOperation("获取楼层")
    @LogAnnotation(operationType = "3", operationContent = "获取楼层")
    public ResponseObject<List<Integer>> getBuildingFloor(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                          @ApiParam(value = "校区id", required = false) @RequestParam(required = false) Integer campusId,
                                                          @ApiParam(value = "楼宇id", required = false) @RequestParam(required = false) Integer buildingId) {
        return ResponseObject.ofSuccess(roomService.getBuildingFloor(taskId, campusId, buildingId));
    }

    @GetMapping("/getRoomListByBuilding")
    @ApiOperation("获取教室")
    @LogAnnotation(operationType = "3", operationContent = "获取教室")
    public ResponseObject<List<Room>> getRoomListByBuilding(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                            @ApiParam(value = "楼宇id", required = false) @RequestParam(required = false) Integer buildingId,
                                                            @ApiParam(value = "校区id", required = false) @RequestParam(required = false) Integer campusId) {
        return ResponseObject.ofSuccess(roomService.getRoomListByBuilding(taskId, campusId, buildingId));
    }

    @GetMapping("/getRoomType")
    @ApiOperation("获取教室类型")
    @LogAnnotation(operationType = "3", operationContent = "获取教室类型")
    public ResponseObject<List<Type>> getRoomType(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                  @ApiParam(value = "校区id", required = false) @RequestParam(required = false) Integer campusId,
                                                  @ApiParam(value = "楼宇id", required = false) @RequestParam(required = false) Integer buildingId,
                                                  @ApiParam(value = "楼层", required = false) @RequestParam(required = false) Integer floor) {
        return ResponseObject.ofSuccess(roomService.getRoomType(taskId, campusId, buildingId, floor));
    }

    @GetMapping("/getCollage")
    @ApiOperation("获取开课单位")
    @LogAnnotation(operationType = "3", operationContent = "获取开课单位")
    public ResponseObject<List<College>> getCollage(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(collegeService.getCollage(taskId));
    }

    @GetMapping("/getCourseType")
    @ApiOperation("获取课程类别")
    @LogAnnotation(operationType = "3", operationContent = "获取课程类别")
    public ResponseObject<List<Course>> getCourseType(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                      @ApiParam(value = "学院id", required = false) @RequestParam(required = false) String baseCollegeId) {
        return ResponseObject.ofSuccess(courseService.getCourseType(taskId, baseCollegeId));
    }

    @GetMapping("/getCourseNature")
    @ApiOperation("获取课程性质")
    @LogAnnotation(operationType = "3", operationContent = "获取课程性质")
    public ResponseObject<List<Integer>> getCourseNature(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                         @ApiParam(value = "学院id", required = false) @RequestParam(required = false) String baseCollegeId,
                                                         @ApiParam(value = "课程类别id", required = false) @RequestParam(required = false) String courseTypeId) {
        return ResponseObject.ofSuccess(courseService.getCourseNature(taskId, baseCollegeId, courseTypeId));
    }

    @ApiOperation("获取考核方式")
    @GetMapping(value = "/assessmentMethod")
    @LogAnnotation(operationType = "3", operationContent = "获取考核方式")
    public ResponseObject<List<AssessmentMethod>> getAssessmentMethod(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        List<AssessmentMethod> result = assessmentMethodService.lambdaQuery().eq(AssessmentMethod::getTaskId, taskId).list();
        if (result.isEmpty()) {
            TaskInfo taskInfo = taskInfoService.getById(taskId);
            AssessmentMethod assessment = new AssessmentMethod()
                    .setAssessmentName("考试")
                    .setTaskId(taskId)
                    .setSchoolId(taskInfo.getSchoolId())
                    .setFlag(1);
            assessmentMethodService.save(assessment);
            result.add(assessment);
            AssessmentMethod assessment1 = new AssessmentMethod()
                    .setAssessmentName("考查")
                    .setTaskId(taskId)
                    .setSchoolId(taskInfo.getSchoolId())
                    .setFlag(1);
            assessmentMethodService.save(assessment1);
            result.add(assessment1);
        }
        return ResponseObject.ofSuccess(result);
    }

    @ApiOperation("添加考核方式")
    @PostMapping(value = "/assessmentMethod")
    @LogAnnotation(operationType = "1", operationContent = "添加考核方式")
    public ResponseObject<AssessmentMethod> addAssessmentMethod(@ApiParam(value = "考核方式实体类", required = true) @Validated @RequestBody AssessmentMethod assessmentMethod) {
        assessmentMethodService.addAssessmentMethod(assessmentMethod);
        return ResponseObject.ofSuccess(assessmentMethod);
    }

    @ApiOperation("删除考核方式")
    @DeleteMapping(value = "/assessmentMethod")
    @LogAnnotation(operationType = "4", operationContent = "删除考核方式")
    public ResponseObject<Object> deleteAssessmentMethod(@ApiParam(value = "考核方式实体类", required = true) @RequestHeader Integer taskId,
                                                         @ApiParam(value = "考核方式id", required = true) @RequestParam Long assessmentMethodId) {
        assessmentMethodService.deleteAssessmentMethod(taskId, assessmentMethodId);
        return ResponseObject.ofSuccess();
    }


}
